<?php

/*
This is library file to payment modules. Cant be reused to 
initalize admin area configuration options.

Copyright (c) 2010 Ingvar Harjaks <ingvarharjaks@gmail.com>
All costs of this development were covered by open source fiendy
company BrightSide OÜ <www.brightside.ee>

Pizza is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Pizza is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Pizza. If not, see <http://www.gnu.org/licenses/>.
*/


abstract class Pizza extends Controller
{
    protected $prefix;
    protected $def_url;


    public function index() {
        $this->load->language('common/pizza');
        $this->load->language("payment/{$this->prefix}");

        $this->document->setTitle($this->language->get('heading_title'));

        $data = array();

        $data['breadcrumbs'] = array();
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_payment'),
            'href' => $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('payment/'.$this->prefix, 'token=' . $this->session->data['token'], 'SSL')
        );



        $data['header']      = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer']      = $this->load->controller('common/footer');

        $data['heading_title']  = $this->language->get('heading_title');
        $data['text_enabled']   = $this->language->get('text_enabled');
        $data['text_disabled']  = $this->language->get('text_disabled');
        $data['text_all_zones'] = $this->language->get('text_all_zones');
        $data['button_save']    = $this->language->get('button_save');
        $data['button_cancel']  = $this->language->get('button_cancel');
        $data['tab_general']    = $this->language->get('tab_general');


        $data['entry_username']       = $this->language->get('entry_username');
        $data['entry_private_key']    = $this->language->get('entry_private_key');
        $data['entry_private_key_pw'] = $this->language->get('entry_private_key_pw');
        $data['entry_public_key']     = $this->language->get('entry_public_key');
        $data['entry_vk_acc']         = $this->language->get('entry_vk_acc');
        $data['entry_vk_name']        = $this->language->get('entry_vk_name');
        $data['entry_bank_url']       = $this->language->get('entry_bank_url');
        $data['entry_order_status']   = $this->language->get('entry_order_status');
        $data['entry_geo_zone']       = $this->language->get('entry_geo_zone');
        $data['entry_status']         = $this->language->get('entry_status');
        $data['entry_sort_order']     = $this->language->get('entry_sort_order');


        $data['prefix']       = $this->prefix;
        $data['def_bank_url'] = $this->def_url;

        $field_names = array(
            "{$this->prefix}_username",
            "{$this->prefix}_key_pub",
            "{$this->prefix}_key_sec",
            "{$this->prefix}_key_sec_pw",
            "{$this->prefix}_vk_acc",
            "{$this->prefix}_vk_name",
            "{$this->prefix}_order_status_id",
            "{$this->prefix}_geo_zone_id",
            "{$this->prefix}_bank_url",
            "{$this->prefix}_status",
            "{$this->prefix}_sort_order"
        );


        foreach ($field_names as $field_name) {
            if (isset($this->request->post[$field_name])) {
                $data[$field_name] = $this->request->post[$field_name];
            } else {
                $data[$field_name] = $this->config->get($field_name);
            }
        }
        /* Apply list of different statuses */
        $this->load->model('localisation/order_status');
        $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

        /* Apply list of different geo zones */
        $this->load->model('localisation/geo_zone');
        $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

        /* And finally, buttons */
        $data['action'] = $this->url->link('payment/'.$this->prefix, 'token=' . $this->session->data['token'], 'SSL');
        $data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');


        if (!empty($this->request->post)) {
            $errors = $this->validate();
            if (empty($errors)) {
                $this->load->model('setting/setting');
                $this->model_setting_setting->editSetting($this->prefix, $this->request->post);
                $this->session->data['success'] = $this->language->get('text_success');
                $this->response->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
            } else {
                $errors = array_filter($errors , function($item){ return !empty($item);});
                $data = array_merge($data , $errors);
            }
        }

        $this->response->setOutput($this->load->view('common/pizza.tpl', $data));
    }

    private function validate()
    {
        $errors = array();

        if (!$this->user->hasPermission('modify', 'payment/' . $this->prefix)) {
            $errors['error_warning'] = $this->language->get('error_permission');
        }
        if (!$this->request->post["{$this->prefix}_username"]) {
            $errors['error_username'] = $this->language->get('error_username');
        }

        if (!is_file((string)$this->request->post["{$this->prefix}_key_pub"])) {
            $errors['error_key_pub'] = $this->language->get('error_public_key');
        }

        if (!is_file((string)$this->request->post["{$this->prefix}_key_sec"])) {
            $errors['error_key_sec'] = $this->language->get('error_private_key');
        }

        if (strlen((string)$this->request->post["{$this->prefix}_vk_acc"]) == 0) {
            $errors['error_vk_acc'] = $this->language->get('error_vk_acc');
        }

        if (strlen((string)$this->request->post["{$this->prefix}_vk_name"]) == 0) {
            $errors['error_vk_name'] = $this->language->get('error_vk_name');
        }

        if (strlen((string)$this->request->post["{$this->prefix}_bank_url"]) == 0) {
            $errors['error_bank_url'] = $this->language->get('error_bank_url');
        }

        return $errors;
    }
}